package uptask

import (
	"game_go/dao/game"
	"log"
	"time"

	"fmt"

	"github.com/astaxie/beego/toolbox"
)

func UpdateGameTask(nIdx int, sName, sTime string) {
	sTaskName := fmt.Sprintf("updateGameTask_%d", nIdx+1)
	updateGameTask := toolbox.NewTask(sTaskName, sTime, func() error {
		_, err := game.UpdateUsed(sName)
		log.Println(fmt.Sprintf("任务执行!!!===>%d", nIdx+1))
		if err != nil {
			log.Println(err)
			go func() {
				//重试三次，总共9分钟
				nTime := []time.Duration{1, 3, 5}
				for index, i := range nTime {
					log.Println("updateGameTask重试：", index)
					_, err := game.UpdateUsed(sTime)
					if err == nil {
						break
					} else {
						log.Println(err)
					}
					time.Sleep(time.Minute * i)
				}
			}()
		}
		return nil
	})
	toolbox.AddTask(sTaskName, updateGameTask)

}
